terminalEditWindow = Ext.extend(Ext.Window,{
		closable:true,
		title: '端终信息',
		width:400,
		layout:'fit',
		createPanel:function() {
			var terminal_level = Ext.data.Record.create([
               {name:'terminal_level_id',type:'string'},
			   {name:'name',type:'string'}           
			   ]);
			var storeLevel = new Ext.data.Store({
				/*proxy: new Ext.data.HttpProxy({
					 url:"index.php?controller=Terminallevel&action=ToJSON"
				}),
				
				record:
				// create reader that reads the project records
				reader: new Ext.data.JsonReader({},[
					 {name:'terminal_level_id',type:'string'},
					 {name:'name',type:'string'}
				]),*/
				proxy: new Ext.data.MemoryProxy(jsCfgData['Id2Discription']['TerminalLevel']),
				reader: new Ext.data.JsonReader({
				   id:'terminal_level_id'
				   }, terminal_level),
				autoLoad:true
			});    
		   

		 storeLevel.load();
			return new Ext.FormPanel({
				//id:'editform',
				frame:true,
				//url:'index.php',
				labelWidth: 90,
				defaults: {width: 140},	
				defaultType: 'textfield',
				autoHeight: true,
				//activeItem:1,
				items: [{
							xtype:'hidden',
							fieldLabel: '终端ID',
							name: 'terminal_id'
						},{
							fieldLabel: '终端名称',
							name: 'name',
							allowBlank:false,
							blankText: '终端名称必须输入！'
						},
						 {
							 xtype:'combo',
							 id:'terminal_level_combo',
							 fieldLabel: '终端等级',
							 name:'level',
							 lazyRender:true,
							 store: storeLevel,
							 displayField:'name',
							 valueField: 'terminal_level_id',
							 hiddenName:'terminal_level',
							 typeAhead: true,
							 mode: 'local',
							 triggerAction: 'all',
							 emptyText:'请选择终端等级',
							 value:1,
							 selectOnFocus:true,
							 editable:false
							 
                         },{
							fieldLabel: '联系人',
							name: 'linkman'
						},{
							fieldLabel: '联系方式',
							name: 'link'
						},{
							fieldLabel: '省',
							name: 's1'
						},{
							fieldLabel: '市',
							name: 's2'
						},{
							fieldLabel: '区/县',
							name: 's3'
						},{
							fieldLabel: '地址',
							name: 'address'
						},{
							fieldLabel: '手机',
							name: 'mobile'
						},{
							xtype:'checkbox',
							fieldLabel: '锁定',
							name: 'lock',
							align:'left'
							//y:5
						},{
							fieldLabel: '积分折扣',
							name: 'discount'
						},{
							fieldLabel: '积分系数',
							name: 'pointFactor'
						},{
							fieldLabel: '计算方式',
							name: 'autoCalculate'
						},{
							fieldLabel: '储值方式',
							name: 'cutsare'
						},{
							xtype:'textarea',
							width:240,
							fieldLabel: '备注',
							name: 'memo'
						}]

			});//end return new Ext.FormPanel
		},
		initComponent:function() {  
			terminalEditWindow.superclass.initComponent.call(this);  
			this.wp = this.createPanel();  
			this.add(this.wp);  
		}
	});//end difine terminalEditWindow

terminal_list = Ext.extend(Ext.Panel,{
	id:'terminal_list',  
	title:'终端列表',  
	//closeAction:'hide',
	autoScroll:true,  
	closable:true,
	layout:'fit', //如果用函数来创建该面板的话,布局必须设置为fit,否则不会显示该面板中的内容

    createPanel:function() {
		var btedit = new  Ext.Toolbar.Button({
				text:'修改',
				icon:'images/icons/fam/icons/application_edit.png',
				cls:'x-btn-text-icon',
				handler:function(){
						var nowSel = Ext.getCmp("terminal-list-grid").getSelectionModel();
						if (nowSel.getCount()!=0){
							var window_id = 'editwindowsform'+nowSel.getSelected().data['terminal_id'];
							var thiswindow = Ext.getCmp(window_id);

							if (!thiswindow){
								var thiswindow = new terminalEditWindow({
										id:window_id,
										iconCls:'my-edit-icon',
										_record:nowSel.getSelected(),
										buttons:[{
													text:'保存',
													handler:function(btn,e){
														var myform = btn.ownerCt.findByType('form')[0].getForm();
														if(myform.isValid()){
															btn.disabled=true;
															myform.doAction('submit',{
																url:'index.php?controller=Terminal&action=Modify',
																method:'post',
																params:'',
																waitMsg:'正在通讯...',
																waitTitle:'请等待',
																success:function(form,action){
																	Ext.Msg.alert('操作',action.result.data);
																	myform.updateRecord(btn.ownerCt._record);
																	btn.disabled=false;
																	btn.ownerCt.close();
																},
																failure:function(){
																	Ext.Msg.alert('操作','保存失败！');
																	btn.disabled=false;
																}	
															});
													   }
													}
												},{
													text:'取消',
													handler:function(btn,e){
														btn.ownerCt.close();
													}
												}]
									});//end var editwindows
								//thiswindow.findByType('form')[0].getForm().url = 'index.php';
							}//end if
							thiswindow.show();
							thiswindow.findByType('form')[0].getForm().loadRecord(nowSel.getSelected());
							
						}else{
							Ext.Msg.alert('注意','请在列表中选择要编辑的终端');
						}
					}//end handler
		});//end var btedit

		var btdelete = new Ext.Toolbar.Button({
				text:'删除',
				icon:'images/icons/fam/icons/delete.png',
				cls:'x-btn-text-icon',
				handler:function(btn,e){
						var nowSel = Ext.getCmp("terminal-list-grid").getSelectionModel();
						if (nowSel.getCount()!=0){
							
							Ext.MessageBox.confirm('删除', '你是否真的要删除?', function(btn){
								if (btn=='yes'){
									var myform = Ext.getCmp("terminal-form").form;
									if(myform.isValid()){
										btn.disabled=true;
										myform.doAction('submit',{
											url:'index.php?controller=Terminal&action=Delete',
											method:'post',
											params:{terminal_id:nowSel.getSelected().data['terminal_id']},
											waitMsg:'正在通讯...',
											waitTitle:'请等待',
											success:function(form,action){
												Ext.Msg.alert('操作',action.result.data);
												//myform.updateRecord(btn.ownerCt._record);
												ds.remove(nowSel.getSelected());
												btn.disabled=false;
											},
											failure:function(){
												Ext.Msg.alert('操作','删除失败！');
												btn.disabled=false;
											}	
										});
									}
								}
						    });
						}else{
							Ext.Msg.alert('注意','请在列表中选择要删除的终端');
						}
					}
			});//end var btdelete
		var btadd = new Ext.Toolbar.Button({
				text:'添加',
				icon:'images/icons/fam/icons/add.png',
				cls:'x-btn-text-icon',
				handler:function(){
					var thiswindow = new terminalEditWindow({
							iconCls:'my-add-icon',
							buttons:[{
										text:'添加',
										handler:function(btn,e){
											var myform = btn.ownerCt.findByType('form')[0].getForm();
											if(myform.isValid()){
												btn.disabled=true;
												myform.doAction('submit',{
													url:'index.php?controller=Terminal&action=Add',
													method:'post',
													params:'',
													waitMsg:'正在通讯...',
													waitTitle:'请等待',
													success:function(form,action){
														
														if (typeof(action.result)!='undefined'){
															Ext.Msg.alert('操作','添加成功');
															var myNewRecord = new TerminalTitleRecord(Ext.decode(action.result.data)[0]);
															ds.add(myNewRecord);
															btn.ownerCt.close();
														}else{
															Ext.Msg.alert('操作','添加失败！');
														}
														
														//Ext.Msg.alert('操作','成功');

														btn.disabled=false;
													},
													failure:function(){
														Ext.Msg.alert('操作','添加失败！');
														btn.disabled=false;
													}	
												});
										   }
										}
									},{
										text:'取消',
										handler:function(btn,e){
											btn.ownerCt.close();
										}
									}]
						});//end var editwindows
						thiswindow.show();
				}//end handler
			});//end var btadd
		var TerminalTitleRecord = Ext.data.Record.create([
				{name:'terminal_id',	type:'int'		},
				{name:'name',			type:'string'	},
				{name:'terminal_level',	type:'int'		},
				{name:'linkman',		type:'string'	},
				{name:'link',			type:'string'	},
				{name:'s1',				type:'int'		},
				{name:'s2',				type:'int'		},
				{name:'s3',				type:'int'		},
				{name:'address',		type:'string'	},
				{name:'mobile',			type:'int'		},
				{name:'lock',			type:'bool'		},
				{name:'discount',		type:'float'	},
				{name:'pointFactor',	type:'float'	},
				{name:'autoCalculate',	type:'bool'		},
				{name:'cutsare',		type:'bool'		},
				{name:'memo',			type:'string'	}
			]);

		var ds = new Ext.data.JsonStore({
			url:'index.php?controller=Terminal&action=List',
			root: 'Result',
			autoLoad:true,
			fields: TerminalTitleRecord	
			});
		// 定制 renderer function 锁定
		function showLock(val){
			if (val==0){
				return '<span style="color:red;">×</span>';
			}else{
				return '<span style="color:green;">√</span>'
			}
		}
		// 定制 renderer function 计算方式
		function calcWay(val){
			if (val==0){
				return '<span style="color:red;">手动</span>';
			}else{
				return '<span style="color:green;">自动</span>'
			}
		}
		// 定制 renderer function 储值方式
		function sareWay(val){
			if (val==0){
				return '<span style="color:red;">No</span>';
			}else{
				return '<span style="color:green;">Yes</span>'
			}
		}
		// 定制 renderer function 级别显示
		function terminalLevelShow(val){
			var a = jsCfgData['Id2Discription']['TerminalLevel'];
			for (k in a)
			{
				if (a[k]['terminal_level_id']==val)
				{
					return a[k]['name'];
				}
			}
			
		}

		// the DefaultColumnModel expects this blob to define columns. It can be extended to provide
		// custom or reusable ColumnModels
		var colModel = new Ext.grid.ColumnModel([
			{header: "终端",		width: 160,	sortable: true, dataIndex: 'name',				locked:false, id:'terminal-name'},
			{header: "级别",		width: 75,	sortable: true, dataIndex: 'terminal_level',	renderer: terminalLevelShow},
			{header: "联系人",		width: 75,	sortable: true, dataIndex: 'linkman'			},
			{header: "锁定",		width: 35,	sortable: true, dataIndex: 'lock',				renderer: showLock},
			{header: "积分折扣",	width: 85,	sortable: true, dataIndex: 'discount'		    },
			{header: "积分系数",	width: 85,	sortable: true, dataIndex: 'pointFactor'		},
			{header: "计算方式",	width: 85,	sortable: true, dataIndex: 'autoCalculate',		renderer: calcWay},
			{header: "储值方式",	width: 85,	sortable: true, dataIndex: 'cutsare',			renderer: sareWay}
		]);
	
	 //	Here is where we create the Form
	
	
	 
		return new Ext.FormPanel({
			id: 'terminal-form',
			frame: true,
			loadMask:'载入中……',
			tbar:[btadd,'-',btdelete,'-',btedit],
			bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: ds,
			displayInfo: true,
            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
            emptyMsg: "无显示数据"
            }),
			labelAlign: 'left',
			//title: 'Company data',
			bodyStyle:'padding:5px',
			width: 750,
			layout: 'column',	// Specifies that the items will now be arranged in columns
			//border: true,
			listeners: {
						bodyresize:function( p,w,h ){
							Ext.getCmp('terminal-list-grid').setHeight(h-50);
						}
			},
			items: [{
				columnWidth: 1,
				layout: 'fit',
				items: {
					xtype: 'grid',
					id:'terminal-list-grid',
					ds: ds,
					cm: colModel,
					sm: new Ext.grid.RowSelectionModel({
						singleSelect: true,
						listeners: {
							rowselect: function(sm, row, rec) {
								//Ext.getCmp("terminal-form").getForm().loadRecord(rec);
							}
						}
					}),
					autoExpandColumn: 'terminal-name',
					height: 325,
					//title:'Company Data',
					border: true,
					listeners: [{
						render: function(g) {
							g.getSelectionModel().selectRow(0);
						},
						delay: 10 // Allow rows to be rendered.
					}]
				}
		
			}]
		});
     },   
    //重装控件初始化函数,在该函数中完成面板中内容的初始化  
    initComponent:function() {  
        terminal_list.superclass.initComponent.call(this);  
        this.fp = this.createPanel();  
        this.add(this.fp);  
    }
	
});